package com.dao;



import java.sql.*;
import java.util.*;
import java.math.*;
import com.vo.*;

 /**

 
  * 

  */
public class TiposUsuarioDAO {
	private Connection connection;

	/**
	 * private ctor
	 */
	private TiposUsuarioDAO(Connection connection) {
		this.connection = connection;
	}
	
	/**
	 * Factory method
	 */
	static public TiposUsuarioDAO create(Connection connection) {
		return new TiposUsuarioDAO(connection);
	}
	/**
	* retrieve one instance of the bean identified by the primary key values passed
	* as arguments
	*/
    public TiposUsuario retrieve(  Integer idTipoUsuario  ) 
    	throws SQLException {
			String sql = "SELECT  id_tipo_usuario, nombre FROM tipos_usuario WHERE 1 = 1 "
		
		 + "AND id_tipo_usuario = ? ";
		PreparedStatement stmt = null;
		TiposUsuario ret = null;
		int idx = 1;
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setObject(idx++,idTipoUsuario,Types.INTEGER);			
			ResultSet rs = stmt.executeQuery();
			if (false == rs.next()) {
				return null;
			}
			ret = makeObject( rs );
			rs.close();
		} finally {
			if (stmt != null)
		  		stmt.close();
		}
		return ret;
    }

	/**
	* Create a new record in the table

 
    * 

    */
    public int insert(TiposUsuario bean) throws SQLException {
    	  validate(bean);
    	      
          String sql = "";
          PreparedStatement stmt = null;

          try {
               sql = "INSERT INTO tipos_usuario (   id_tipo_usuario  , nombre  ) " + 
               	"VALUES (  ?  , ?  ) ";
			stmt = connection.prepareStatement(sql);
			int idx = 1;
			stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);
			stmt.setString(idx++,bean.getNombre());
			int rowcount = stmt.executeUpdate();
			return rowcount;
          } finally {
              if (stmt != null)
                  stmt.close();
          }
    }

	/**

 
    * 

    */
    public int[] insertAll(Collection beans) throws SQLException {
          PreparedStatement stmt = null;
          String sql = "INSERT INTO tipos_usuario (   id_tipo_usuario  , nombre  ) " + 
               	"VALUES (  ?  , ?  ) ";
		  stmt = connection.prepareStatement(sql);
          try {
			  for (Iterator it = beans.iterator(); it.hasNext();) {
		      	TiposUsuario bean = (TiposUsuario)it.next();
	    	  	validate(bean);	    	      
				int idx = 1;
				stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);
				stmt.setString(idx++,bean.getNombre());
								
				stmt.addBatch();
			}
			return stmt.executeBatch();
          } finally {
              if (stmt != null)
                  stmt.close();
          }
    }
	
	/**

 
    * 

    */
    public int[] updateAll(Collection beans) throws SQLException {
          PreparedStatement stmt = null;
          String sql = "UPDATE tipos_usuario SET " + 
							" id_tipo_usuario = ? " +
							",  nombre = ? " +
							" WHERE "  
							+ " id_tipo_usuario = ? ";
		  stmt = connection.prepareStatement(sql);
          try {
			  for (Iterator it = beans.iterator(); it.hasNext();) {
		      	TiposUsuario bean = (TiposUsuario)it.next();
	    	  	validate(bean);	    	      
				int idx = 1;
				stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);
				stmt.setString(idx++,bean.getNombre());
				
			  stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);			  				
				stmt.addBatch();
			}
			return stmt.executeBatch();
          } finally {
              if (stmt != null)
                  stmt.close();
          }
    }
	/**

 
    * 

    */
    public int update(TiposUsuario bean) 
          throws SQLException {
		  validate(bean);
          String sql = "UPDATE tipos_usuario SET " + 
							" id_tipo_usuario = ? " +
							",  nombre = ? " +
							" WHERE "  
							+ " id_tipo_usuario = ? ";
				
          PreparedStatement stmt = null;
		  int idx = 1;
          try {
              stmt = connection.prepareStatement(sql);
			  stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);
			  stmt.setString(idx++,bean.getNombre());
			  
			  stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);			  
              int rowcount = stmt.executeUpdate();
			  return rowcount;
          } finally {
              if (stmt != null)
                  stmt.close();
          }
    }



	/**

 
    * 

    */
    public int delete(TiposUsuario bean) throws SQLException {
          String sql = "DELETE FROM tipos_usuario WHERE 1=1 "
							+ " AND id_tipo_usuario = ? "
			;
			
          PreparedStatement stmt = null;
		  int idx = 1;
          try {
              stmt = connection.prepareStatement(sql);
			  stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);
			  
              int rowcount = stmt.executeUpdate();
			return rowcount;
          } finally {
              if (stmt != null)
                  stmt.close();
          }
    }


	/**

 
    * 

    */
    public int deleteAll() throws SQLException {

          String sql = "DELETE FROM tipos_usuario";
          PreparedStatement stmt = null;
          try {
              stmt = connection.prepareStatement(sql);
              int rowcount = stmt.executeUpdate();
              return rowcount;
          } finally {
              if (stmt != null)
                  stmt.close();
          }
    }

 
    public int countAll() throws SQLException {

          String sql = "SELECT count(*) FROM tipos_usuario";
          PreparedStatement stmt = null;
          ResultSet result = null;
          int allRows = 0;

          try {
              stmt = connection.prepareStatement(sql);
              result = stmt.executeQuery();
              if (result.next())
                  allRows = result.getInt(1);
          } finally {
              if (result != null)
                  result.close();
              if (stmt != null)
                  stmt.close();
          }
          return allRows;
    }


	/**

 
    * 

    */
    public Collection retrieveAll(TiposUsuario bean) throws SQLException {
          List searchResults;
          StringBuffer sql = new StringBuffer("SELECT * FROM tipos_usuario WHERE 1=1 ");

		  if (bean.getIdTipoUsuario() != null ) {
		    	sql.append( " AND id_tipo_usuario=? " );
			}
		  if (bean.getNombre() != null ) {
		    	sql.append( " AND nombre=? " );
			}
		  
			PreparedStatement stmt = connection.prepareStatement(sql.toString());
			int idx = 1;			
		  if (bean.getIdTipoUsuario() != null ) {
	      		stmt.setObject(idx++,bean.getIdTipoUsuario(),Types.INTEGER);}			    
		  if (bean.getNombre() != null ) {
	      		stmt.setString(idx++,bean.getNombre());}			    
		  
		 ResultSet rs = stmt.executeQuery();
		 if (rs.next()) {
		     searchResults = new ArrayList();
		 } else {
		 	return Collections.EMPTY_LIST;
		 }
		 TiposUsuario d;
		 do {
		 	d = makeObject( rs );
		 	searchResults.add( d );
		 } while (rs.next());

          return searchResults;
    }



	/**

 
    * 

    */
    static private TiposUsuario makeObject(ResultSet result) throws SQLException {
          TiposUsuario bean = new TiposUsuario();
/*
		  Integer id_tipo_usuario result;
		  String nombre result;
*/
		  bean.setIdTipoUsuario((Integer)get(Integer.class, result.getObject("id_tipo_usuario")));
		  bean.setNombre(result.getString("nombre"));
		return bean;
    }


	/**

 
    * 

    */
    static public Object get(Class cls, Object source) {
        if (source == null) return null;
        if (cls.isInstance( source.getClass())) return source;
        if (cls.equals(String.class)) return source.toString();
        if (source instanceof Number) {
            Number n = (Number)source;
            if (cls.equals(Integer.class)) return new Integer( n.intValue() );
            if (cls.equals(Long.class)) return new Long( n.longValue() );
            if (cls.equals(Float.class)) return new Float( n.floatValue() );
            if (cls.equals(Double.class)) return new Double( n.doubleValue() );
            if (cls.equals(Short.class)) return new Short( n.shortValue() );
            if (cls.equals(Byte.class)) return new Byte( n.byteValue() );
            if (cls.equals(BigDecimal.class)) return new BigDecimal( n.doubleValue() );
        }

        if (cls.equals( Character.class)) {
            String str = source.toString();
            if (str.length() == 0) return null;
            return new Character(str.charAt( 0 ));
        }
        
        return source;
    }

	/**

 
    * 

    */
	private void validate( TiposUsuario bean ) throws SQLException {
	// NULL values
		if (bean.getIdTipoUsuario() == null) {
			throw new SQLException("Column id_tipo_usuario, property idTipoUsuario of type TiposUsuario cannot be NULL");
		}
 		if (bean.getNombre() == null) {
			throw new SQLException("Column nombre, property nombre of type TiposUsuario cannot be NULL");
		}
 		if (bean.getNombre() != null && bean.getNombre().length() > 150 ) {
			throw new SQLException("Column nombre, property nombre of type TiposUsuario should be less than 150 chars");
		}
	}

}


             



